let mongoose = require('mongoose');

module.exports = {
    async editExportData(options){
        let ClickCensusModel = mongoose.models['ClickCensus'];
        let data = await ClickCensusModel.findOne({_id: options.censusId});
        if(!data) return;
        data.exportCount = options.exportCount || 0;
        data.sellPrice = options.sellPrice || 0;
        data.exportProfit = (data.exportCount * data.sellPrice).toFixed(2);
        data.clickProfit = data.newOpenCount ? (data.exportProfit / data.newOpenCount).toFixed(2) : 0;
        await data.save();
    },
    async checkData(date){
        let ClickCensusModel = mongoose.models['ClickCensus'];
        let oneDay = 24 * 60 * 60 * 1000;
        let addDate = new Date(new Date(date).getTime() - oneDay).toLocaleDateString();
        console.log('addDate =', addDate);
        let data = await ClickCensusModel.find({addDate});
        if(data.length) return;
        let FriendLinkClickModel = mongoose.models['FriendLinkClick'];
        let friendLinkClicks = await FriendLinkClickModel.aggregate([
            {
                $match: {addDate}
            },
            {
                $group: {
                    _id: '$appName',
                    count: {$sum: '$count'},
                    openCount: {$sum: '$openCount'},
                    newCount: {$sum: '$newCount'},
                    newOpenCount: {$sum: '$newOpenCount'},
                }
            }
        ]);
        // console.log('data =', friendLinkClicks);
        let preDate = new Date(new Date(date).getTime() - 2 * oneDay).toLocaleDateString();
        for(let i = 0 ; i < friendLinkClicks.length ; i++){
            let appName = friendLinkClicks[i]._id;
            let preCensus = await ClickCensusModel.findOne({appName, addDate: preDate});
            let sellPrice = preCensus ? preCensus.sellPrice : 0;
            // console.log('sellPrice =', preDate, sellPrice);
            delete friendLinkClicks[i]._id;
            await ClickCensusModel.create(Object.assign({appName, addDate, timeTick: new Date(addDate).getTime(), sellPrice}, friendLinkClicks[i]));
        }
    },
    async findDataByAddDate(addDate){
        // let addDate = options.addDate;
        let ClickCensusModel = mongoose.models['ClickCensus'];
        let data = await ClickCensusModel.find({addDate});
        data.sort((param1, param2)=>{
            return (param1.appName).localeCompare(param2.appName,"zh");
        });
        // console.log('findDataByAddDate data =', data);
        return data;
    },
    async findDataByAppName(options){
        let ClickCensusModel = mongoose.models['ClickCensus'];
        let obj = {
            appName: options.appName,
            timeTick: {
                $gte: new Date(new Date().toLocaleDateString()).getTime() - 30 * 24 * 60 * 60 * 1000,
                $lte: new Date(new Date().toLocaleDateString()).getTime()
            }
        };
        if(!!options.time1){
            obj.timeTick.$gte = parseInt(options.time1);
        }
        if(!!options.time2){
            obj.timeTick.$lte = parseInt(options.time2);
        }
        let data = await ClickCensusModel.find(obj).sort({timeTick: -1});
        // console.log('findDataByAppName data =', data);
        return data;
    },
};